#include "demonstratewidget.h"
#include "ui_demonstratewidget.h"
#include <QRegExp>

#define MAX_JOINT_SIZE 50

static double jValue[MAX_JOINT_SIZE];
ThreeLable::ThreeLable(QWidget*parent):QWidget(parent)
{
    QHBoxLayout *hlay = new QHBoxLayout(this);
    labFront = new QLabel;
    labBack = new QLabel;
    labMid = new QLabel;
    labFront->setText("lab1");
    labMid->setText("lab2");
    labBack->setText("lab3");
    hlay->addWidget(labFront);
    hlay->addWidget(labMid);
    hlay->addWidget(labBack);
    hlay->setContentsMargins(4,0,4,0);
    hlay->setSpacing(3);
}

DemonstrateWidget::DemonstrateWidget(QWidget *parent,uint64_t num,mat jPos) :
    QWidget(parent),
    ui(new Ui::DemonstrateWidget)
{
    ui->setupUi(this);
    ui->edtPosNum->setValidator(new QIntValidator(0,0x4000,this));
    setPosName(num);
    ui->twdJoint->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);   // 设置子项目等宽、、
    ui->twdJoint->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    info.pos = jPos;

    int length = info.pos.n_rows;
    ui->twdJoint->setColumnCount(length);
    for(int i = 0;i < length;i++)
    {
        QTableWidgetItem *wid = new QTableWidgetItem(QString("%1").arg(info.pos(i,0),4,'f',3));
        ui->twdJoint->setItem(0,i,wid);
    }
}

DemonstrateWidget::DemonstrateWidget(QWidget* parent,DemoMsg msg):
    QWidget(parent),
    ui(new Ui::DemonstrateWidget)
{
    ui->setupUi(this);
    ui->edtPosNum->setValidator(new QIntValidator(0,0x4000,this));
    setPosName(msg.posNum);

    ui->twdJoint->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);   // 设置子项目等宽、、
    ui->twdJoint->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    info = msg;
    ui->edtSpeed->setText(QString("%1").arg(info.speed));
    ui->edtCNT->setText(QString("%1").arg(info.cnt));
    ui->edtAcc->setText(QString("%1").arg(info.acc));
    ui->edtPosNum->setText(QString("%1").arg(info.posNum));
    int length = info.pos.n_rows;
    ui->twdJoint->setColumnCount(length);
    for(int i = 0;i < length;i++)
    {
        QTableWidgetItem *wid = new QTableWidgetItem(QString("%1").arg(info.pos(i,0),4,'f',3));
        ui->twdJoint->setItem(0,i,wid);
    }
}

DemoMsg DemonstrateWidget::getInfo()
{
    if(ui->edtCNT->text().length()!=0){
        info.cnt = QString(ui->edtCNT->text()).toFloat();
    }
    if(ui->edtSpeed->text().length() != 0){
        info.speed = QString(ui->edtSpeed->text()).toFloat();
    }
    if(ui->edtAcc->text().length() != 0){
        info.acc = QString(ui->edtAcc->text()).toFloat();
    }
    info.moType = (FormMotion)ui->cbxType->currentIndex();
    getPos();
    return info;
}

void DemonstrateWidget::setPosName(uint64_t num)
{
    ui->edtPosNum->setText(QString("%1").arg(num));
}

cJSON* DemonstrateWidget::getJsonInfo()
{
    if(jsonInfo != NULL)
    {
        cJSON_Delete(jsonInfo);
        jsonInfo = NULL;
    }
    getInfo();
    jsonInfo = cJSON_CreateObject();
    uint32_t num = ui->edtPosNum->text().toUInt();

    cJSON_AddNumberToObject(jsonInfo,"PosNumber",num);
    cJSON_AddNumberToObject(jsonInfo,"Speed",info.speed);
    cJSON_AddNumberToObject(jsonInfo,"Cnt",info.cnt);
    cJSON_AddNumberToObject(jsonInfo,"Acc",info.acc);

    char* pMoveType;
    switch(info.moType){
    case moveLine:
        pMoveType = (char*)"LineType";
        break;
    case moveJoint:
        pMoveType = (char*)"JointType";
        break;
    case moveACC:
        pMoveType = (char*)"AccType";
        break;
    default:
        return NULL;
    }
    cJSON_AddStringToObject(jsonInfo,"Type",pMoveType);
    for(int i = 0;i < info.pos.n_rows && i < MAX_JOINT_SIZE;i++) jValue[i] = info.pos(i,0);
    cJSON* jointValue = cJSON_CreateDoubleArray(jValue,info.pos.n_rows);
    cJSON_AddItemToObject(jsonInfo,"Value",jointValue);
    return jsonInfo;
}


DemonstrateWidget::~DemonstrateWidget()
{
    if(jsonInfo != NULL)
    {
        cJSON_Delete(jsonInfo);
        jsonInfo = NULL;
    }
    delete ui;
}


int DemonstrateWidget::getNum()
{
    return ui->edtPosNum->text().toInt();
}

mat DemonstrateWidget::getPos()
{
    for(int i = 0;i < ui->twdJoint->columnCount();i++)
    {
        info.pos(i,0) = ui->twdJoint->item(0,i)->text().toDouble();
    }
    return info.pos;
}

void DemonstrateWidget::setPos(mat pos)
{
    pos.print();
    for(int i = 0;i < ui->twdJoint->columnCount();i++)
    {
        ui->twdJoint->item(0,i)->setText(QString("%1").arg(pos(i,0),4,'f',3));
    }
}

void DemonstrateWidget::on_twdJoint_clicked(const QModelIndex &index)
{
    emit windowClicked(this);
    qDebug()<<"selected num:"<< ui->edtPosNum->text();
}

void DemonstrateWidget::on_edtPosNum_editingFinished()
{
    emit numChanged(this,ui->edtPosNum->text().toInt());
}

bool DemonstrateWidget::setJsonInfo(cJSON *root,DemoMsg* msg)
{
    cJSON* temp;
    temp = cJSON_GetObjectItem(root,"PosNumber");
    if(temp == NULL){
        qDebug()<<"PosNumber is not exist!";
    }
    msg->posNum = temp->valueint;


    temp = cJSON_GetObjectItem(root,"Speed");
    if(temp == NULL){
        qDebug()<<"PosNumber is not exist!";
    }
    msg->speed = temp->valueint;

    temp = cJSON_GetObjectItem(root,"Cnt");
    if(temp == NULL){
        qDebug()<<"Cnt is not exist!";
    }
    msg->speed = temp->valueint;

    temp = cJSON_GetObjectItem(root,"Acc");
    if(temp == NULL){
        qDebug()<<"Acc is not exist!";
        return msg;
    }
    msg->acc = temp->valueint;

    temp = cJSON_GetObjectItem(root,"Type");
    if(temp == NULL){
        qDebug()<<"Type is not exist!";
        return msg;
    }
    if(strcmp(temp->valuestring,"LineType"))msg->moType = moveLine;
    else if(strcmp(temp->valuestring,"JointType"))msg->moType = moveJoint;
    else if(strcmp(temp->valuestring,"AccType"))msg->moType = moveACC;

    temp = cJSON_GetObjectItem(root,"Value");
    if(temp == NULL){
        qDebug()<<"Value is not exist!";
        return msg;
    }
    cJSON* arrayTemp;
    int len = cJSON_GetArraySize(temp);
    msg->pos.reshape(len,1);
    for(int i = 0;i < len;i++)
    {
        arrayTemp = cJSON_GetArrayItem(temp,i);
        if(temp == NULL){
            qDebug()<<"Value at"<<i<<"is not exist!";
            return msg;
        }
        msg->pos(i,0) = arrayTemp->valuedouble;
    }
    return true;
}

